﻿using System.Collections.Generic;
using System.Reflection;
using Autofac;
using Autofac.Integration.Mvc;
using SmallBusinessKit.DataAccess;
using SmallBusinessKit.DataAccess.Infrastructure;
using SmallBusinessKit.DataAccess.Interfaces;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit
{
    /// <summary>
    /// Autofac module configuration class
    /// </summary>
    /// <remarks>http://autofac.readthedocs.org/en/latest/configuration/index.html</remarks>
    public class SmallBusinessKitModule : Autofac.Module
    {
        #region Properties

        public string DataProvider      { get; set; }
        public string CategoriesXml     { get; set; }
        public string ItemsXml          { get; set; }
        public string NewsXml           { get; set; }
        public string PeopleXml         { get; set; }
        public string TestimonialsXml   { get; set; }

        #endregion

        #region Methods

        protected override void Load(ContainerBuilder builder)
        {
            var xmlData = new XmlDataParser(CategoriesXml, ItemsXml, PeopleXml, NewsXml, TestimonialsXml);

            base.Load(builder);

            builder.RegisterControllers(Assembly.GetExecutingAssembly()).InstancePerRequest();  //Note: chg to .InstancePerDependancy() if using @Url.Action()
            builder.RegisterType<SmallBusinessContext>().InstancePerRequest();

            var domains = Assembly.Load("SmallBusinessKit");
            builder.RegisterAssemblyTypes(domains)
                   .Where(t => (!t.Name.EndsWith("Repository")))
                   .AsImplementedInterfaces()
                   .InstancePerRequest();

            builder.RegisterInstance(xmlData).As<IXmlDataParser>();

            if (DataProvider.ToLower().Equals("sql"))
            {
                builder.RegisterType<DatabaseFactory>()
                   .As<IDatabaseFactory>()
                   .InstancePerRequest();

                builder.RegisterType<UnitOfWork>()
                       .As<IUnitOfWork>()
                       .InstancePerRequest();

                builder.RegisterAssemblyTypes(typeof(RepositoryBase<>).Assembly)
                       .Where(t => ((!t.Name.StartsWith("Xml")) && t.Name.EndsWith("Repository")))
                       .AsImplementedInterfaces()
                       .InstancePerRequest();

                builder.Register(c => new CreateDatabaseIfNotExistsWithSeed(c.Resolve<IXmlDataParser>()));
                builder.Register(c => new SmallBusinessContext(c.Resolve<CreateDatabaseIfNotExistsWithSeed>()));
            }
            else
            {
                // this release pre-creates instances for the lifetime of the app, 
                // rather than re-reading/parsing the xml files on each request
                builder.RegisterInstance(xmlData.ParseCategories()).As<IList<Category>>();
                builder.RegisterInstance(xmlData.ParseItems()).As<IList<Item>>();
                builder.RegisterInstance(xmlData.ParsePeople()).As<IList<Person>>();
                builder.RegisterInstance(xmlData.ParseNews()).As<IList<News>>();
                builder.RegisterInstance(xmlData.ParseTestimonials()).As<IList<Testimonial>>();
                
                builder.RegisterAssemblyTypes(typeof(XmlRepositoryBase<>).Assembly)
                       .Where(t => ((t.Name.StartsWith("Xml")) && t.Name.EndsWith("Repository")))
                       .AsImplementedInterfaces()
                       .InstancePerRequest();
            }

            builder.RegisterFilterProvider();

            // done...
        }

        #endregion
    }
}
